-- Lua Battle System - 装备

local Logger = require("utils.logger")
local Item = require("items.item")

local Equipment = Item:new({
    -- 装备特有属性
    type = "equipment",
    stackable = false,
    
    -- 装备类型
    equipment_slot = "", -- weapon, armor, accessory
    equipment_type = "", -- sword, axe, staff, shield, etc.
    
    -- 装备属性加成
    stats = {
        attack = 0,
        defense = 0,
        magic_attack = 0,
        magic_defense = 0,
        speed = 0,
        luck = 0,
        max_hp = 0,
        max_mp = 0
    },
    
    -- 装备特殊效果
    special_effects = {},
    
    -- 装备等级要求
    level_requirement = 1
})

-- 装备构造函数
function Equipment:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    o:init()
    return o
end

-- 初始化装备
function Equipment:init()
    Item.init(self)
    self.type = "equipment"
    self.stackable = false
    self.equipment_slot = ""
    self.equipment_type = ""
    self.stats = {
        attack = 0,
        defense = 0,
        magic_attack = 0,
        magic_defense = 0,
        speed = 0,
        luck = 0,
        max_hp = 0,
        max_mp = 0
    }
    self.special_effects = {}
    self.level_requirement = 1
    
    Logger.debug("装备初始化完成: " .. self.name)
end

-- 检查装备是否可以装备
function Equipment:can_equip(user)
    -- 检查等级要求
    if user.level < self.level_requirement then
        Logger.warn(user.name .. " 等级不足，无法装备 " .. self.name .. "，需要等级: " .. self.level_requirement)
        return false
    end
    
    return true
end

-- 装备时的效果
function Equipment:on_equip(user)
    -- 应用属性加成
    self:apply_stats(user, 1)
    
    -- 应用特殊效果
    self:apply_special_effects(user, "equip")
    
    Logger.info(self.name .. " 装备效果已应用")
end

-- 卸下时的效果
function Equipment:on_unequip(user)
    -- 移除属性加成
    self:apply_stats(user, -1)
    
    -- 移除特殊效果
    self:apply_special_effects(user, "unequip")
    
    Logger.info(self.name .. " 装备效果已移除")
end

-- 应用属性加成
function Equipment:apply_stats(user, multiplier)
    -- 应用基础属性加成
    if self.stats.attack ~= 0 then
        user.attack = user.attack + (self.stats.attack * multiplier)
    end
    
    if self.stats.defense ~= 0 then
        user.defense = user.defense + (self.stats.defense * multiplier)
    end
    
    if self.stats.magic_attack ~= 0 then
        user.magic_attack = user.magic_attack + (self.stats.magic_attack * multiplier)
    end
    
    if self.stats.magic_defense ~= 0 then
        user.magic_defense = user.magic_defense + (self.stats.magic_defense * multiplier)
    end
    
    if self.stats.speed ~= 0 then
        user.speed = user.speed + (self.stats.speed * multiplier)
    end
    
    if self.stats.luck ~= 0 then
        user.luck = user.luck + (self.stats.luck * multiplier)
    end
    
    -- 应用最大生命值和魔法值加成
    if self.stats.max_hp ~= 0 then
        user.max_hp = user.max_hp + (self.stats.max_hp * multiplier)
        -- 如果是装备，恢复相应的生命值
        if multiplier > 0 then
            user.hp = user.hp + self.stats.max_hp
        else
            -- 如果是卸下，确保生命值不超过当前最大生命值
            user.hp = math.min(user.hp, user.max_hp)
        end
    end
    
    if self.stats.max_mp ~= 0 then
        user.max_mp = user.max_mp + (self.stats.max_mp * multiplier)
        -- 如果是装备，恢复相应的魔法值
        if multiplier > 0 then
            user.mp = user.mp + self.stats.max_mp
        else
            -- 如果是卸下，确保魔法值不超过当前最大魔法值
            user.mp = math.min(user.mp, user.max_mp)
        end
    end
end

-- 应用特殊效果
function Equipment:apply_special_effects(user, action)
    for _, effect in ipairs(self.special_effects) do
        if effect[action] then
            effect[action](user, self)
        end
    end
end

-- 装备物品（重写父类方法）
function Equipment:equip(user)
    if not self:can_equip(user) then
        return false
    end
    
    return Item.equip(self, user)
end

-- 获取装备状态（扩展）
function Equipment:get_state()
    local state = Item.get_state(self)
    state.equipment_slot = self.equipment_slot
    state.equipment_type = self.equipment_type
    state.stats = self.stats
    state.level_requirement = self.level_requirement
    return state
end

-- 导出模块
return Equipment